package com.market.cash.ui.presenter

import com.market.cash.configs.CashRootConfigs
import com.market.cash.http.ApiService
import com.market.cash.http.IHttpCallback
import com.market.cash.pro.ProPresenter
import com.market.cash.ui.view.ICashRootSplashView
import com.market.cash.utils.helper.CashRootAnalysisHelper
import com.market.cash.utils.helper.SharedHelper
import com.market.cash.utils.permission.Permission
import com.market.cash.utils.permission.XXPermissions
import org.json.JSONObject
import java.util.TimeZone

/**
 * CashRoot
 * @Author wwdeng
 * @CreateTime: 2024/10/24 13:45
 * @Annotation：贷款申请 Presenter 实现
 */
class CashRootSplashPresenter(private val iView: ICashRootSplashView) : ProPresenter(iView) {

    /**
     * 获取地区转发
     */
    fun loadCashRootTimeZoneAction() {
        iView.showCashRootLoading()
        val params = JSONObject()
        params.put("p", CashRootConfigs.CASHROOT_APPNAME)
        params.put("tz", TimeZone.getDefault().id)
        val httpUrl = ApiService.CASHROOT_BASE_URL + ApiService.API_URL.CASHROOT_API_TIMEZONE_URL
        doCashRootPostAction(httpUrl, params,object : IHttpCallback {
            override fun onRequestSuccess(data: JSONObject) {
                iView.showCashRootTimeZoneRequestData(data.optString("data"))
            }
            override fun onRequestError(error: JSONObject) {
            }
            override fun onRequestFinish() {
            }
        })
    }

    /**
     * 获取APP配置信息
     */
    fun loadCashRootConfigsAction() {
        val params = JSONObject().apply {
            put("product",CashRootConfigs.CASHROOT_APPNAME)
            put("path",ApiService.API_PATH.CASHROOT_API_APP_INFO_PATH)
        }
        val httpUrl = ApiService.getHttpUrl(ApiService.API_URL.CASHROOT_API_COMMON_URL)
        doCashRootPostAction(httpUrl, params,object : IHttpCallback{
            override fun onRequestSuccess(data: JSONObject) {
                iView.hideCashRootLoading()
                val obj = data.optJSONObject("obj")
                //保存所有数据
                if (obj != null) {
                    SharedHelper.saveCashRootSPData(SharedHelper.CashRoot_SPKEY.CASHROOT_COMMON,obj.toString())
                }

                // TODO: PhoneState为必须申请的权限
                if (!XXPermissions.isGranted(iView.getAct(), Permission.READ_PHONE_STATE)){
                    //进入权限页
                    iView.showCashRootPermissionPage()
                } else {
                    //已登录 - 进入主页
                    if (SharedHelper.getCashRootSPBoolean(SharedHelper.CashRoot_SPKEY.CASHROOT_LOGIN)){
                        //初始化analysis lib 赋值 用户id 和 token
                        CashRootAnalysisHelper.initAnalysisLibAtLoginSuccess()
                        iView.showCashRootMainPage()
                    } else {//进入登录页
                        iView.showCashRootLoginPage()
                    }
                }
            }
            override fun onRequestError(error: JSONObject) {
            }
            override fun onRequestFinish() {
                iView.hideCashRootLoading()
            }
        })
    }
}